<?php

namespace app\api\controller;



use addons\epay\library\Service;
use app\api\model\hotel\HotelFacility;
use app\api\model\hotel\HotelSettlement;
use app\common\controller\Api;
use app\api\model\hotel\Hotel as HotelModel;

/**
 * 酒店接口
 */
class Hotel extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //酒店列表
    public function lists()
    {
        $hotel_model = new HotelModel();
        $params = input();
        $list = $hotel_model->lists($params);

        $this->success('',$list);
    }

    //酒店列表地图
    public function maplists()
    {
        $hotel_model = new HotelModel();
        $params = input();
        $list = $hotel_model->maplists($params);

        $this->success('',$list);
    }

    //酒店详情
    public function info()
    {
        $hotel_id = input('hotel_id',0);
        $lat = input('lat','');
        $lng = input('lng','');

        if (empty($hotel_id))$this->error('缺少参数酒店id');

        $info = HotelModel::where(['hotel_id'=>$hotel_id])
            ->field('hotel_id,hotel_name,image,label,address,lat,lng,room_area,room_people_num,reception,mobile,explain,facility_id')
            ->find();

        if ($info[ 'lng' ] && $info[ 'lat' ]&&$lng&&$lat) {
            $distance = getDistance((float) $info[ 'lng' ], (float) $info[ 'lat' ], (float) $lng, (float) $lat);
            $info[ 'distance' ] = round($distance / 1000,1);
        } else {
            $info[ 'distance' ] = 0;
        }

        $facility_list = HotelFacility::where('facility_id','in',$info[ 'facility_id' ])
            ->field('name,image')
            ->order('weigh desc')->select();

        $info[ 'facility_list' ] = $facility_list;

        $this->success('',$info);
    }

    //结算
    public function settlement()
    {
        $model = new HotelSettlement();
        // 获取当前日期
        $today = date("Y-m-d");

        // 将当前日期转换为当天开始的时间戳
        $time = strtotime($today);
        $res   = $model->settlement($time);

        $this->success('请求成功',$res);
    }

    public function notify()
    {
        $paytype = "wechat";
        $pay = Service::checkNotify($paytype);
        if (!$pay) {
            echo '签名错误';
            return;
        }
        $data = $pay->verify();
        $out_trade_no = $data['out_trade_no'];
        $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;

        $model = new \app\api\model\hotel\HotelOrder();

        $res = $model->notify($out_trade_no,$payamount);

        $this->success('提交成功',$res);
    }
}